﻿#include <QFile>
#include <QUrl>
#include "CHeader.h"
#include "CSqlOper.h"
#include "CTaskDetailsDlg.h"
#include "CItemDetailDlg.h"
#include "CPanItemDetailDlg.h"
#include "CCotdetQueryDlg.h"

CTaskDetailsDlg::CTaskDetailsDlg(QString sSceneGuid, QString sTaskExecGuid, QString sTaskName, CRBaseData* pDataBase,QString sTaskExecDesc, QWidget *parent)
	: m_sSceneGuid(sSceneGuid), m_sTaskExecGuid(sTaskExecGuid),m_sTaskName(sTaskName), m_pBasicData(pDataBase),m_sTaskExecDesc(sTaskExecDesc),QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	setStyleSheetFile();
	initData();
	initUI();
	initConnect();
	queryItem();
}

CTaskDetailsDlg::~CTaskDetailsDlg()
{}

void CTaskDetailsDlg::setStyleSheetFile()
{
	//设置对话框标题
	this->setWindowTitle(A2T("任务详情"));
	QString sQss = setUiStyle("styleSheet");
	QString sMyqss = styleSheet();
	QString sMerged = sQss + "\n" + sMyqss; // 后面的会覆盖前面的相同规则	
	setStyleSheet(sMerged);
}

void CTaskDetailsDlg::initConnect()
{
	connect(ui.widget, SIGNAL(sigShowItem(QString,QString,QString,QString,int,int,int)), this, SLOT(slotShowItem(QString, QString, QString, QString, int, int, int)));
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CTaskDetailsDlg::slotClose);
}

void CTaskDetailsDlg::initData()
{
	//巡检项名称传感器
	{
		QVector<QSqlRecord> vecRecord;
		QString sErrMsg = "";
		selectPloItemSensorBySceneID(m_sSceneGuid, vecRecord, sErrMsg);
		for (int i = 0; i < vecRecord.size(); i++)
		{
			QString sGuid = vecRecord[i].value("GUID").toString();
			QString sName = vecRecord[i].value("POLL_ITEM_NAME").toString();
			m_mapItemName.insert(sGuid, sName);
		}
	}

	//巡检项名称云台
	{
		QVector<QSqlRecord> vecRecord;
		QString sErrMsg = "";
		selectPloItemCamerBySceneID(m_sSceneGuid, vecRecord, sErrMsg);
		for (int i = 0; i < vecRecord.size(); i++)
		{
			QString sGuid = vecRecord[i].value("GUID").toString();
			QString sName = vecRecord[i].value("POLL_ITEM_NAME").toString();
			m_mapItemName.insert(sGuid, sName);
		}
	}

	//巡检点名称
	{
		m_pBasicData->m_pMutex->lock();
		for (QMap<QString, CPolPoint*>::iterator itor = m_pBasicData->m_pPolArea->m_mapPolPoint.begin();
			itor != m_pBasicData->m_pPolArea->m_mapPolPoint.end(); itor++)
		{
			CPolPoint* pPoint = itor.value();
			m_mapPointName.insert(pPoint->m_sPointGuid, pPoint->m_sPointName);
			m_mapPointLoc.insert(pPoint->m_sPointGuid, pPoint->m_dPointLocation);
		}
		m_pBasicData->m_pMutex->unlock();
	}

	//巡检项设备类型编码  0图像相机 1红外相机 2声音传感器 3一氧化碳  4.可燃气体 5.PM25 6.PM10 7:温度传感器
	{
		m_mapItemDeviceType.insert(enCamer, A2T("图像相机"));
		m_mapItemDeviceType.insert(enIR, A2T("红外相机"));
		m_mapItemDeviceType.insert(enVoice, A2T("声音传感器"));
		m_mapItemDeviceType.insert(enCO, A2T("一氧化碳"));
		m_mapItemDeviceType.insert(enGas, A2T("可燃气体"));
		m_mapItemDeviceType.insert(enPM25, A2T("PM2.5"));
		m_mapItemDeviceType.insert(enPM10, A2T("PM10"));
		m_mapItemDeviceType.insert(enTemp, A2T("温度传感器"));
		m_mapItemDeviceType.insert(enHum, A2T("湿度传感器"));
		m_mapItemDeviceType.insert(enUltrasonic, A2T("超声波传感器"));
	}

	{
		m_mapUnit.insert(enIR, A2T("℃"));
		m_mapUnit.insert(enVoice, A2T("db"));
		m_mapUnit.insert(enCO, A2T("ppm"));
		m_mapUnit.insert(enGas, A2T("ppm"));
		m_mapUnit.insert(enPM25, A2T("ug/m3"));
		m_mapUnit.insert(enPM10, A2T("ug/m3"));
		m_mapUnit.insert(enTemp, A2T("℃"));
		m_mapUnit.insert(enHum, A2T("%RH"));
		m_mapUnit.insert(enCamer, A2T(""));
		m_mapUnit.insert(enUltrasonic, A2T(""));
	}
	
}

void CTaskDetailsDlg::initUI()
{
	ui.widget->InitUi(2);
}

void CTaskDetailsDlg::queryItem()
{
	ui.labelTaskExecDesc->setText(m_sTaskExecDesc);
	//首先查询传感器巡检项记录表
	QVector<QVector<QString>>vecStr;
	int nRow = 0;
	{
		QVector<QSqlRecord> vecRecord;
		QString sErrMsg = "";
		selectSensorItemExecByTaskExecID(m_sSceneGuid, m_sTaskExecGuid, vecRecord, sErrMsg);
		for (int i = 0; i < vecRecord.size(); i++)
		{
			QVector<QString> vecRow;
			vecRow.push_back(QString::number(nRow));//序号
			vecRow.push_back(m_sTaskName);//任务名称
			vecRow.push_back(m_mapPointName[vecRecord[i].value("POINT_GUID").toString()]);//巡检点名称
			vecRow.push_back(vecRecord[i].value("ROLLER_CODE").toString());//托辊号
			vecRow.push_back(m_mapItemName[vecRecord[i].value("ITEM_GUID").toString()]);//巡检项名称
			vecRow.push_back(QString::number(0));//设备类型0：本体传感器
			vecRow.push_back(m_mapItemDeviceType[vecRecord[i].value("ITEM_TYPE_CODE").toInt()]);//巡检类型
			vecRow.push_back(vecRecord[i].value("START_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));//执行时间
			vecRow.push_back(vecRecord[i].value("SENSOR_DATA").toString());//执行结果
			vecRow.push_back(vecRecord[i].value("DATA_THRESHOLD_H").toString());//报警阀值
			vecRow.push_back("");//结果图片
			vecRow.push_back(vecRecord[i].value("GUID").toString());//巡检项执行记录Guid
			vecRow.push_back(QString::number(m_mapPointLoc[vecRecord[i].value("POINT_GUID").toString()]));
			vecRow.push_back(vecRecord[i].value("IS_COTDET").toString());//是否连续检测型
			vecRow.push_back(vecRecord[i].value("ITEM_TYPE_CODE").toString());//巡检项类型编码
			vecRow.push_back(m_mapUnit[vecRecord[i].value("ITEM_TYPE_CODE").toInt()]);//对应的单位
			vecRow.push_back(QString::number(0));
			vecStr.push_back(vecRow);
			nRow++;
		}
	}
	int nCotdetIr = 0;//红外持续监测数量
	int nCotdetVoice = 0;//声音持续监测数量
	int nCotdetIrAlarm = 0;//红外持续监测报警数
	int nCotdetVoiceAlarm = 0;//声音持续监测报警数
	int nCotdetCamer = 0;//图像相机持续巡检数
	int nCotdetCamerAlarm = 0;//图像相机持续巡检报警数
	QVector<QVector<QString>> vecIr;
	QVector<QVector<QString>> vecVoive;
	//查询云台巡检项
	{
		QVector<QSqlRecord> vecRecord;
		QString sErrMsg = "";
		selectImageItemExecByTaskExecID(m_sSceneGuid, m_sTaskExecGuid, vecRecord, sErrMsg);
		for (int i = 0; i < vecRecord.size(); i++)
		{
			//首先判断是否是持续监测巡检结果,并判断是否发生报警
			if (vecRecord[i].value("IS_COTDET").toInt() == 1 && vecRecord[i].value("ITEM_TYPE_CODE").toInt() == enIR)
			{
				if (vecRecord[i].value("ALARM_LEVEL").toInt() != 0)
				{
					nCotdetIrAlarm++;
				}
				nCotdetIr++;
				continue;
			}
			if (vecRecord[i].value("IS_COTDET").toInt() == 1 && vecRecord[i].value("ITEM_TYPE_CODE").toInt() == enVoice)
			{
				if (vecRecord[i].value("ALARM_LEVEL").toInt() != 0)
				{
					nCotdetVoiceAlarm++;
				}
				nCotdetVoice++;
				continue;
			}
			if (vecRecord[i].value("IS_COTDET").toInt() == 1 && vecRecord[i].value("ITEM_TYPE_CODE").toInt() == enCamer)
			{
				if (vecRecord[i].value("ALARM_LEVEL").toInt() != 0)
				{
					nCotdetCamerAlarm++;
				}
				nCotdetCamer++;
				continue;
			}
			QVector<QString> vecRow;
			vecRow.push_back(QString::number(nRow));//序号
			vecRow.push_back(m_sTaskName);//任务名称
			vecRow.push_back(m_mapPointName[vecRecord[i].value("POINT_GUID").toString()]);//巡检点名称
			vecRow.push_back(vecRecord[i].value("ROLLER_CODE").toString());//托辊号
			vecRow.push_back(m_mapItemName[vecRecord[i].value("ITEM_GUID").toString()]);//巡检项名称
			vecRow.push_back(QString::number(1));//设备类型1：云台
			vecRow.push_back(m_mapItemDeviceType[vecRecord[i].value("ITEM_TYPE_CODE").toInt()]);//巡检类型
			vecRow.push_back(vecRecord[i].value("START_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));//执行时间
			if (vecRecord[i].value("ITEM_TYPE_CODE").toInt() == enCamer)//图像相机
			{
				vecRow.push_back("/");//执行结果，使用最高温作为执行结果
				vecRow.push_back("/");//报警阀值
			}
			else if(vecRecord[i].value("ITEM_TYPE_CODE").toInt() == enIR)
			{
				vecRow.push_back(vecRecord[i].value("TEMP_MAX").toString());//执行结果，使用最高温作为执行结果
				vecRow.push_back(vecRecord[i].value("DATA_THRESHOLD_H").toString());//报警阀值
			}
			else if (vecRecord[i].value("ITEM_TYPE_CODE").toInt() == enVoice)
			{
				vecRow.push_back(vecRecord[i].value("VOICE_DATA").toString());//执行结果
				vecRow.push_back(vecRecord[i].value("DATA_THRESHOLD_H").toString());//报警阀值
			}

			vecRow.push_back(vecRecord[i].value("RESULT_HTTP_URL").toString());//结果图片
			vecRow.push_back(vecRecord[i].value("GUID").toString());//巡检项执行记录Guid
			vecRow.push_back(QString::number(m_mapPointLoc[vecRecord[i].value("POINT_GUID").toString()]));
			vecRow.push_back(vecRecord[i].value("IS_COTDET").toString());//是否连续检测型
			vecRow.push_back(vecRecord[i].value("ITEM_TYPE_CODE").toString());//巡检项类型编码
			vecRow.push_back(m_mapUnit[vecRecord[i].value("ITEM_TYPE_CODE").toInt()]);//对应的单位
			vecRow.push_back(QString::number(0));
			vecStr.push_back(vecRow);
			nRow++;
		}
	}

	//将vecStr排序
	qSort(vecStr.begin(), vecStr.end(), [](const QVector<QString>& vec1, const QVector<QString>& vec2) {
		if (vec1[12].toInt() != vec2[12].toInt()) {
			return vec1[12].toInt() < vec2[12].toInt();
		}
		else
		{
			return vec1[5].toInt() < vec2[5].toInt();
		}
	});

	//最后将红外和声音持续检测加入
	if (nCotdetIr > 0)//有持续检测
	{
		QVector<QString> vecRow;
		vecRow.push_back(QString::number(nRow));//序号
		vecRow.push_back(m_sTaskName);//任务名称
		vecRow.push_back("/");//巡检点名称
		vecRow.push_back("/");//托辊号
		vecRow.push_back(A2T("红外持续监测"));//巡检项名称
		vecRow.push_back(QString::number(1));//设备类型1：云台
		vecRow.push_back(A2T("红外持续监测"));//巡检类型
		vecRow.push_back("/");//执行时间
		vecRow.push_back(A2T("共%1项/报警%2项").arg(nCotdetIr).arg(nCotdetIrAlarm));//执行结果，使用最高温作为执行结果
		vecRow.push_back("/");//报警阀值
		vecRow.push_back("");//结果图片
		vecRow.push_back("");//巡检项执行记录Guid
		vecRow.push_back("");
		vecRow.push_back(QString::number(1));//是否连续检测型
		vecRow.push_back(QString::number(enIR));//巡检项类型编码
		vecRow.push_back(m_mapUnit[enIR]);//对应的单位
		vecRow.push_back(QString::number(nCotdetIrAlarm));//红外持续巡检报警数
		vecStr.push_back(vecRow);
		nRow++;
	}
	
	if (nCotdetVoice > 0)
	{
		QVector<QString> vecRow;
		vecRow.push_back(QString::number(nRow));//序号
		vecRow.push_back(m_sTaskName);//任务名称
		vecRow.push_back("/");//巡检点名称
		vecRow.push_back("/");//托辊号
		vecRow.push_back(A2T("声音持续监测"));//巡检项名称
		vecRow.push_back(QString::number(1));//设备类型1：云台
		vecRow.push_back(A2T("声音持续监测"));//巡检类型
		vecRow.push_back("/");//执行时间
		vecRow.push_back(A2T("共%1项/报警%2项").arg(nCotdetVoice).arg(nCotdetVoiceAlarm));//执行结果，使用最高温作为执行结果
		vecRow.push_back("/");//报警阀值
		vecRow.push_back("");//结果图片
		vecRow.push_back("");//巡检项执行记录Guid
		vecRow.push_back("");
		vecRow.push_back(QString::number(1));//是否连续检测型
		vecRow.push_back(QString::number(enVoice));//巡检项类型编码
		vecRow.push_back(m_mapUnit[enVoice]);//对应的单位
		vecRow.push_back(QString::number(nCotdetVoiceAlarm));//声音持续巡检报警数
		vecStr.push_back(vecRow);
		nRow++;
	}

	if (nCotdetCamer > 0)
	{
		QVector<QString> vecRow;
		vecRow.push_back(QString::number(nRow));//序号
		vecRow.push_back(m_sTaskName);//任务名称
		vecRow.push_back("/");//巡检点名称
		vecRow.push_back("/");//托辊号
		vecRow.push_back(A2T("图像相机持续监测"));//巡检项名称
		vecRow.push_back(QString::number(1));//设备类型1：云台
		vecRow.push_back(A2T("图像相机持续监测"));//巡检类型
		vecRow.push_back("/");//执行时间
		vecRow.push_back(A2T("共%1项/报警%2项").arg(nCotdetCamer).arg(nCotdetCamerAlarm));//执行结果，使用最高温作为执行结果
		vecRow.push_back("/");//报警阀值
		vecRow.push_back("");//结果图片
		vecRow.push_back("");//巡检项执行记录Guid
		vecRow.push_back("");
		vecRow.push_back(QString::number(1));//是否连续检测型
		vecRow.push_back(QString::number(enCamer));//巡检项类型编码
		vecRow.push_back(m_mapUnit[enCamer]);//对应的单位
		vecRow.push_back(QString::number(nCotdetCamerAlarm));//图像持续巡检报警数
		vecStr.push_back(vecRow);
		nRow++;
	}
	ui.widget->InitData(2, vecStr);
}

void CTaskDetailsDlg::slotShowItem(QString sTaskName,QString sItemExecGuid, QString sItemName,QString sPointName, int nDeviceType, int nIsCotdet,int nItemTypeCode)
{
	if (nIsCotdet == 0)//不是连续检测巡检项结果
	{
		if (nDeviceType == 0)//传感器
		{
			CItemDetailDlg oper(sItemExecGuid, sItemName, sPointName);
			oper.exec();
		}
		else if (nDeviceType == 1)//云台
		{
			CPanItemDetailDlg oper(sItemExecGuid, sItemName, sPointName);
			oper.exec();
		}
	}
	else if(nIsCotdet == 1)//是连续检测巡检项结果
	{
		CCotdetQueryDlg oper(m_pBasicData,m_sSceneGuid,m_sTaskExecGuid,sTaskName, nItemTypeCode);
		oper.exec();
	}
}

void CTaskDetailsDlg::slotClose()
{
	this->close();
}
